package chat.chit.tit.mit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.search.UserSearchManager;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import chat.chit.tit.mit.adapter.ChatAdapter;
import chat.chit.tit.mit.adapter.ContactsAdapter;
import chat.chit.tit.mit.model.ChatContentEntity;
import chat.chit.tit.mit.model.PersonEntity;
import chat.chit.tit.mit.variables.Variables;

@SuppressLint("ValidFragment")
public class ContactSearchFragment extends Fragment {

	private ListView lvContent;
	private ContactsAdapter contactAdapter;
	private List<PersonEntity> listContact;
	private ImageView btnSearch;
	private EditText editSearch;
	private XMPPConnection conn = Variables.connection;
	private Handler mHandler = new Handler();

	@SuppressLint("ValidFragment")
	public ContactSearchFragment() {
		listContact = new ArrayList<PersonEntity>();
		// getMessage(conn);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.activity_search_contacts,
				container, false);
		btnSearch = (ImageView) rootView.findViewById(R.id.btn_search);
		editSearch = (EditText) rootView.findViewById(R.id.ed_search);
		lvContent = (ListView) rootView.findViewById(R.id.list_contact_search);
		contactAdapter = new ContactsAdapter(getActivity(), listContact);
		lvContent.setAdapter(contactAdapter);
		btnSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String contentSearch = editSearch.getText().toString();
				if (!contentSearch.trim().equals("")) {

					try {
						UserSearchManager search = new UserSearchManager(conn);
						Form searchForm = search.getSearchForm("search.anhtai");

						Form answerForm = searchForm.createAnswerForm();
						answerForm.setAnswer("Username", true);

						answerForm.setAnswer("search", contentSearch);
						ReportedData data = search.getSearchResults(answerForm,
								"search." + conn.getServiceName());
						if (data.getRows() != null) {
							Iterator<Row> it = data.getRows();
							while (it.hasNext()) {
								Row row = it.next();
								Iterator iterator = row.getValues("jid");
								if (iterator.hasNext()) {
									String value = iterator.next().toString();
									Log.i("Iteartor values......", " " + value);
								}
								// Log.i("Iteartor values......"," "+value);
							}
//							Toast.makeText(ContactSearch,
//									"Username Exists", Toast.LENGTH_SHORT)
//									.show();
						} else {
							System.out.println("its null");
						}
					} catch (XMPPException e) {
						// TODO Auto-generated catch block
						Log.i("Iteartor values......"," "+e);
						e.printStackTrace();
					}
				}
			}
		});
		return rootView;
	}
}
